/****************************************************************************\
 * @file    game_math.c
 * @author  马新硕
 * @date    2022/09/20
 * @brief   游戏内用到的数学计算
 * @note    
 ******************************************************************************
 * @attent  
 ****************************************************************************/
#include "game_math.h"
#include "math.h"
#include "stdint.h"

//取最小值
uint16_t min(uint16_t a, uint16_t b)
{
  if(a <= b){return a;}
  else{return b;}
}

//求解直线距离
uint16_t straight_distance(uint16_t ax, uint16_t ay, uint16_t bx, uint16_t by)
{
  return (uint16_t)sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by));
}

//计算向量 a->b
void vector_calculation(float* vector, uint16_t ax, uint16_t ay, uint16_t bx, uint16_t by)
{
  vector[0] = (float)(bx - ax);
  vector[1] = (float)(by - ay);
}

//计算向量夹角
float vector_costheta(float* vector1, float* vector2)
{
  float cos_theta;
  cos_theta = (float)(vector1[0]*vector2[0] + vector1[1]*vector2[1]) / 
                     (sqrt(vector1[0]*vector1[0]+vector1[1]*vector1[1])+sqrt(vector2[0]*vector2[0]+vector2[1]*vector2[1]));
  return cos_theta;
}

//计算向量点积
float vector_dot_product(float* vector1, float* vector2)
{
  return vector1[0]*vector2[0] + vector1[1]*vector2[1];
}

//随机数
void randomNum(uint8_t *num)
{
	*num = (*num+1)%2;
}

//四舍五入
int16_t roundNum(float num)
{
  if(num >= 0)
    return (int16_t)(num + 0.5f);
  else
    return (int16_t)(num - 0.5f);
}
